package it.ipermemory;

import it.ipermemory.graphic.ImageButtonBox;
import it.ipermemory.logic.Thing;
import it.ipermemory.logic.Game;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;

/**
 * Activity that represents the logic of the easy game.
 * 
 * @author Stefano Tonello (stefano.tonello8821@gmail.com)
 * @version 1.0
 */
public class GameActivityHelp extends GameActivity {

	/**
	 * flag to know if the visual help is used
	 */
	private boolean helpVisual = false;
	/**
	 * flag to know if the audio help is used
	 */
	private boolean helpAudio = false;
	/**
	 * the number of the visual help
	 */
	private int numberHelpVisual = 0;
	/**
	 * the number of the audio help
	 */
	private int numberHelpAudio = 0;
	/**
	 * ImageButton to discover the box
	 */
	protected ImageButton discoverButton;
	/**
	 * ImageButton to use the audio help
	 */
	protected ImageButton audioHelpButton;
	/**
	 * ImageButton to use the visual help
	 */
	protected ImageButton visualHelpButton;
	/**
	 * icon of the button to discover the box
	 */
	protected Bitmap bitmapDiscover = null;
	/**
	 * icon disabled of the button to discover the box
	 */
	protected Bitmap bitmapDiscoverDisabled = null;
	/**
	 * icon of the button to use the audio help
	 */
	protected Bitmap bitmapAudioHelp = null;
	/**
	 * icon disabled of the button to use the audio help
	 */
	protected Bitmap bitmapAudioHelpDisabled = null;
	/**
	 * icon of the button to use the visual help
	 */
	protected Bitmap bitmapVisualHelp = null;
	/**
	 * icon disabled of the button to use the visual help
	 */
	protected Bitmap bitmapVisualHelpDisabled = null;
	/**
	 * flag to know if the table is enabled
	 */
	protected Boolean enabled = true;
	/**
	 * it represents the ImageView of the last box
	 */
	private ImageView lastImageButtonBox = null;

	/**
	 * An object that initializes the game screen
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_game);

		onCreateGameActivity();

		discoverButton = (ImageButton) findViewById(R.id.discover_button);
		audioHelpButton = (ImageButton) findViewById(R.id.audio_help);
		visualHelpButton = (ImageButton) findViewById(R.id.visual_help);

		resID = res.getIdentifier("show_box_disabled", "drawable",
				getPackageName());
		bitmapDiscoverDisabled = BitmapFactory.decodeResource(res, resID);

		resID = res.getIdentifier("show_box", "drawable", getPackageName());
		bitmapDiscover = BitmapFactory.decodeResource(res, resID);

		resID = res.getIdentifier("help_sound_disabled", "drawable",
				getPackageName());
		bitmapAudioHelpDisabled = BitmapFactory.decodeResource(res, resID);

		resID = res.getIdentifier("help_sound", "drawable", getPackageName());
		bitmapAudioHelp = BitmapFactory.decodeResource(res, resID);

		resID = res.getIdentifier("help_image_disabled", "drawable",
				getPackageName());
		bitmapVisualHelpDisabled = BitmapFactory.decodeResource(res, resID);

		resID = res.getIdentifier("help_image", "drawable", getPackageName());
		bitmapVisualHelp = BitmapFactory.decodeResource(res, resID);

		/**
		 * the listener of the button to discover the card
		 */
		discoverButton.setOnClickListener(new OnClickListener() {

			@SuppressWarnings("deprecation")
			public void onClick(View v) {
				lastImageButtonBox.setAlpha(255);
				enabled = false;
				game.setAttempts(game.getAttempts() + 1);
				Integer attemptsInteger = Integer.valueOf(game.getAttempts());
				attempts.setText(attemptsInteger.toString());
				ivSupport.setImageBitmap(secondBitmap);
				Handler handler1 = new Handler();
				handler1.postDelayed(new Runnable() {
					public void run() {
						discover();
						enabled = true;
					}
				}, 1000);
			}
		});

		/**
		 * the listener of the button to use the audio help
		 */
		audioHelpButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				chrono.stop();
				elapsedMilliseconds = chrono.getBase()
						- SystemClock.elapsedRealtime();

				helpAudio = true;

				Intent intent = new Intent(GameActivityHelp.this,
						SoundActivity.class);
				intent.putExtra(Game.GAME_THING_CARD, gbSupport.getLogicBox()
						.getId());
				startActivity(intent);
			}
		});

		/**
		 * the listener of the button to use the visual help
		 */
		visualHelpButton.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				chrono.stop();
				elapsedMilliseconds = chrono.getBase()
						- SystemClock.elapsedRealtime();

				helpVisual = true;

				Intent intent = new Intent(GameActivityHelp.this,
						ImageActivity.class);
				intent.putExtra(Game.GAME_THING_CARD, gbSupport.getLogicBox()
						.getId());
				startActivity(intent);
			}
		});

		discoverButton.setVisibility(View.VISIBLE);
		discoverButton.setImageBitmap(bitmapDiscoverDisabled);
		discoverButton.setClickable(false);

		audioHelpButton.setVisibility(View.VISIBLE);
		audioHelpButton.setImageBitmap(bitmapAudioHelpDisabled);
		audioHelpButton.setClickable(false);

		visualHelpButton.setVisibility(View.VISIBLE);
		visualHelpButton.setImageBitmap(bitmapVisualHelpDisabled);
		visualHelpButton.setClickable(false);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_game, menu);
		return true;
	}

	/**
	 * method to manage the touch of the boxes
	 * 
	 * @param v
	 *            represents the view of the game
	 */
	protected void manageTouchBox(View v) {
		if (enabled) {
			manageOnClick(v);
		}
	}

	/**
	 * method to manage the tap of the user
	 */
	@SuppressWarnings("deprecation")
	protected void manageSecondTouch() {

		if (lastImageButtonBox != null && lastImageButtonBox != ivSupport) {
			lastImageButtonBox.setAlpha(255);
		}

		lastImageButtonBox = ivSupport;

		helpAudio = false;
		helpVisual = false;

		discoverButton.setImageBitmap(bitmapDiscover);
		discoverButton.setClickable(true);
		audioHelpButton.setImageBitmap(bitmapAudioHelp);
		audioHelpButton.setClickable(true);
		visualHelpButton.setImageBitmap(bitmapVisualHelp);
		visualHelpButton.setClickable(true);

	}

	/**
	 * method to show the box selected
	 */
	@SuppressWarnings("deprecation")
	protected void showBox(ImageButtonBox ivSupport) {
		this.ivSupport = ivSupport;

		Thing thingSupport = Game.parserXML.getThingById(gbSupport
				.getLogicBox().getId());

		if (thingSupport == null) {
			mexError();
		} else {
			if (game.getFirstBox() == null) {
				firstBitmap = BitmapFactory.decodeResource(
						res,
						getResources().getIdentifier(thingSupport.getImage(),
								"drawable", getPackageName()));
			} else {
				secondBitmap = BitmapFactory.decodeResource(
						res,
						getResources().getIdentifier(thingSupport.getImage(),
								"drawable", getPackageName()));
			}
		}
		if (game.getFirstBox() == null) {
			ivSupport.setImageBitmap(firstBitmap);
		} else {
			ivSupport.setAlpha(150);
		}

	}

	/**
	 * method to discover the image of the last box selceted
	 */
	public void discover() {

		if ((firstGbSupport.getLogicBox()).equals(gbSupport.getLogicBox()) == true) {
			game.setScore(game.getScore() + 100);
			Integer scoreInteger = Integer.valueOf(game.getScore());
			score.setText(scoreInteger.toString());

			idToPass = gbSupport.getLogicBox().getId();

			showImageButton.setClickable(true);
			showImageButton.setImageBitmap(bitmapCard);
			;
			actionsAfterDiscover();
			actionsToReset();

		} else {
			ivFirstSupport.setImageBitmap(bitmapBackBox);
			ivSupport.setImageBitmap(bitmapBackBox);

			table_layout.setEnabled(true);

			firstGbSupport.setUncovered(false);
			gbSupport.setUncovered(false);
			actionsToReset();
			showImageButton.setClickable(false);
			showImageButton.setImageBitmap(bitmapCardDisabled);
		}

		discoverButton.setImageBitmap(bitmapDiscoverDisabled);
		discoverButton.setClickable(false);

		audioHelpButton.setImageBitmap(bitmapAudioHelpDisabled);
		audioHelpButton.setClickable(false);

		visualHelpButton.setImageBitmap(bitmapVisualHelpDisabled);
		visualHelpButton.setClickable(false);

	}

	/**
	 * method to manage the operation onResume of the activity
	 */
	protected void onResume() {
		super.onResume();

		if (endGame) {
			showImageButton.setClickable(false);
			showImageButton.setImageBitmap(bitmapCardDisabled);

		} else {
			if (cardActivityShown) {
				chrono.setBase(SystemClock.elapsedRealtime()
						+ elapsedMilliseconds);
				chrono.start();

				showImageButton.setClickable(false);
				showImageButton.setImageBitmap(bitmapCardDisabled);
			}
			if (helpVisual) {
				chrono.setBase(SystemClock.elapsedRealtime()
						+ elapsedMilliseconds);
				chrono.start();

				numberHelpVisual++;
				visualHelpButton.setClickable(false);
				visualHelpButton.setImageBitmap(bitmapVisualHelpDisabled);
			}
			if (helpAudio) {
				chrono.setBase(SystemClock.elapsedRealtime()
						+ elapsedMilliseconds);
				chrono.start();

				numberHelpAudio++;
				audioHelpButton.setClickable(false);
				audioHelpButton.setImageBitmap(bitmapAudioHelpDisabled);
			}
		}

	}

	/**
	 * method to calculate the score of the game
	 * 
	 * @param elapsedSeconds
	 *            the seconds elapsed from the start of the game
	 */
	protected void calculateScoreToPass(int elapsedSeconds) {
		game.calculateScore(elapsedSeconds, numberHelpVisual, numberHelpAudio);
	}

	/**
	 * method to set the end of the game and to hide some buttons
	 */
	public void endGame() {
		endGame = true;
		discoverButton.setVisibility(View.GONE);
		visualHelpButton.setVisibility(View.GONE);
		audioHelpButton.setVisibility(View.GONE);
	}

}
